<!DOCTYPE html>
<html>
  <head>
    <title>y86 Simulator</title>
	<style>
		body {
			font: 12px Helvetica, Arial, Sans;
		}
		h1, p {
			padding: 0% 1%;
		}
		.buttons {
			text-align: center;
		}
		.col3 {
			font-weight: bold;
			float: left;
			padding: 1%;
		}
		.code {
			font-size: 12px;
			font-family: monospace;
			width: 100%;
			height: 320px;
			overflow: auto;
		}
		.info { height: auto; }

		.left { text-align: left; }
		.center { text-align: center; }
		.right { text-align: right; }

	</style>
  </head>
  <body>
	<h1>y86 Simulator</h1>
	<p>Start coding and click assemble or copy/paste your object file to assembled and hit execute.</p>
		
	<div class='buttons'>
		<button id="assem" class="btn btn-primary">Assemble</button>
		<button id="exec" class="btn">Execute</button>
		<button id="assemexec" class="btn">Assemble + Execute</button>
	</div>
	
	<div class="col3" style="width: 36%">
		<textarea id="y86" class="code"># Execution begins at address 0
.pos 0
init: irmovl Stack, %esp # Setup stack pointer
irmovl Stack, %ebp # Set up base pointer
call Main # Execute main program
halt # Terminate program

# Array of 4 elements
.align 4
array: .long 0xd
.long 0xc0
.long 0xb00
.long 0xa000

Main: pushl %ebp
rrmovl %esp,%ebp
irmovl $4,%eax
pushl %eax # Push 4
irmovl array,%edx
pushl %edx # Push array
call Sum # Sum(array, 4)
rrmovl %ebp,%esp
popl %ebp
ret

# int Sum(int *Start, int Count)
Sum: pushl %ebp
rrmovl %esp,%ebp
mrmovl 8(%ebp),%ecx # ecx = Start
mrmovl 12(%ebp),%edx # edx = Count
xorl %eax,%eax # sum = 0
andl %edx,%edx # Set condition codes
je End
Loop: mrmovl (%ecx),%esi # get *Start
addl %esi,%eax # add to sum
irmovl $4,%ebx #
addl %ebx,%ecx # Start++
irmovl $-1,%ebx #
addl %ebx,%edx # Count--
jne Loop # Stop when 0
End: rrmovl %ebp,%esp
popl %ebp
ret

# The stack starts here and grows to lower addresses
.pos 0x100
Stack:</textarea> Code
	</div>
	<div class="col3 center" style="width: 42%">
		<textarea id='assembled' class='code'></textarea> Assembled
	</div>
	<div class="col3 right" style="width: 16%">
		<textarea id='result' class='code'></textarea> Result
	</div>
	<p>&nbsp;</p>
	<div class="center"> By Victor Aguilar | 
	<script>
		var e = [118, 105, 99, 116, 111, 114, 97, 103, 117, 105, 108, 97, 114, 57, 48, 32, 123, 97, 116, 125, 32, 103, 109, 97, 105, 108, 32, 99, 111, 109], r = '';
		for (c in e) {
			r += String.fromCharCode(e[c]);
		}
		document.write(r);
	</script>

	<p>&nbsp;</p>
	<a href="http://y86tutoring.wordpress.com/y86-ia/">Instruction Set</a> | <a href="http://github.com/vaguilar/js-y86/">View on Github</a>
	</div>

	<script src="y86.js"></script>
	<script src="assem.js"></script>
	<script src="instr.js"></script>
	<script src="general.js"></script>
	<script src="syntax.js"></script>
	<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
	<script>
		//Assemble when button is clicked
		$('#assem').click(function(){
			var assembled = ASSEMBLE($('#y86').val());
			$('#assembled').val(assembled);
		});

		$('#exec').click(function(){
			var result = toByteArray($('#assembled').val()),
				registers = '';
			result = EXECUTE(result);
			registers = reg2str(REG);
	
			$('#result').val(registers + '\n' + result);
		});

		$('#assemexec').click(function(){
			$('#assem').click();
			$('#exec').click();	
		});
	</script>

  </body>
</html>


